package com.gitee.attendance.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebFilter(urlPatterns = "/attendance.html", filterName = "notPhoneFilter")
public class NotPhoneFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(NotPhoneFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String userAgent = request.getHeader("User-Agent");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        logger.debug(userAgent);
        if (userAgent != null && (userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("Android") != -1)) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            response.sendRedirect("/notPhone.html");
        }
    }

    @Override
    public void destroy() {

    }
}